﻿@{
    ViewBag.Title = "Create";
    //Layout = "~/Views/Shared/_LayoutKoJS.cshtml";
}
@*style="position:absolute;top:150px;z-index:4;"*@

<div>
    <div>
        <table id="gridTable2" style="width: 1000px"></table>
    </div>
    <div class="clear"></div>
    <div style="margin-left:40%;">
        <input type="button" id="RuleRelat" onclick="RuleRelat()" value="提交" />
        <input type="button" id="RuleAdd" onclick="RuleAdd()" value="添加新指标" />
    </div>
</div>

@*@section scripts {*@
<script type="text/javascript">
    //var list = '';
    var dialogRDInit = false;
    var ChkData = {};
    $(document).ready(function () {

        $("#gridTable2").jqGrid({
            @*url: '/API'+'@Url.Action("GetRule", "InspectionRuleAPI")',
            datatype: 'json',*@
            url: '',
            datatype: 'local',
            rowNum: 100,
            //colNames: ['', 'ID', '名称', '类型', '阀值','类型','值',''],
            colNames: ['ID', '名称', '类型', '阀值', '类型', '值', ''],
            colModel: [
                      //{ index: "Choice", name: "Choice", formatter: CIChoice },
                          { index: "1", name: "RequestID", sorttype: "text", hidden: true },
                          { index: "536870916", name: "cChr_RuleName", sorttype: "text", editable: true, editoptions: { size: 35 }, editrules: { required: true }, width: 300 },
                          { index: "536870915", name: "cChr_RuleType", sorttype: "text", width: 190, formatter: formatType, editable: true, editoptions: { size: 35 }, edittype: 'select', editoptions: { value: "Input:输入框;Dropdownlist:下拉框;RadioBox:单选框" }, editrules: { required: true }, width: 150 },
                          { index: "536870926", name: "cChr_Threshold", sorttype: "text", editable: true, editoptions: { size: 35 },width:70 },
                          { index: "536870913", name: "cChr_Permission", sorttype: "text", editable: true, editoptions: { size: 35 }, hidden: true },
                          { index: "536870914", name: "cChr_RuleValue", sorttype: "text", editable: true, editoptions: { size: 35, defaultValue: "正常;不正常;" }, hidden: true, editrules: { edithidden: true } },
                          { index: "Operation", name: "Operation", formatter: RuleOperation, width: 75 },
            ],
            multiselect: true,
            //multikey: "ctrlKey",
            viewrecords: true,
            jsonReader: {
                repeatitems: false,
                id: "rule"
            },
            //我写的
            pager: '#pagered',
            sortname: 'id',
            viewrecords: true,
            sortorder: "asc",
            capition: "Search Example",
            //  editurl : '/API'+'(),

            mtype: "GET",
            autowidth: true,
            postData: {
                Type: '@ViewBag.Type',
                Permission:'@ViewBag.Permission'
            }, //发送数据
            prmNames: {
                id: 'Type'
            },
            scrollOffset: 0,
            height: "230",
            width: "980",
            gridview: true,

            //全选
            onSelectAll: function (rowid, status) {
            },

            //单选
            onSelectRow: function (rowid, status) {
            },
            ondblClickRow: function (rowid) {
               
            },
            gridComplete: function () {
            },
            loadComplete: function () {
                //勾上已经选中的项
                @*$.ajax("/API/SMGXJPlanTicketRuleRelatedAPI?TicketID=" + '@ViewBag.TicketID' + "&Name=" + '@ViewBag.Name' + "&Type=" + '@ViewBag.Type', {
                    type: "GET", contentType: "application/json", async: false,
                    success: function (result) {
                        if ($("#gridTable2").getDataIDs().length > 0 && result.length > 0) {
                            for (i = 0; i < result.length; i++) {
                                $("[title=" + result[i].cChr_RuleID + "]").prev().children().prop("checked", true)
                            }
                        }
                        if ($("#gridTable2").getDataIDs().length == result.length)
                            $("#cb_gridTable2").prop("checked", true);
                    }
                });*@
                var dataSplit = '@ViewBag.Name'.split(';');
                for (var i = 0; i < dataSplit.length; i++) {
                    if (dataSplit[i] != "") {
                        $("#gridTable2 [title=" + dataSplit[i] + "]").prev().children().prop("checked", true)
                        $("#gridTable2").setSelection($("#gridTable2 [title=" + dataSplit[i] + "]").parent().attr("id"), true);
                        if ($("#gridTable2").getDataIDs().length == dataSplit.length)
                            $("#cb_gridTable2").prop("checked", true);
                    }
                }
            }

        });

        LoadHosts2();
    });

    function LoadHosts2() {
        var data = {};
        data["cChr_Char03"] = '@ViewBag.Type';
        data["cChr_Permission"] = '@ViewBag.Permission';
        jQuery("#gridTable2").jqGrid("setGridParam", {
            url: "/API/InspectionRuleAPI/GetRule", //设置表格的url
            datatype: "json", //设置数据类型
            async: false,
            postData: { strJson: data }
        }).trigger("reloadGrid");
    }

    function formatType(cellvalue, options, rowObject) {
        if (options.rowId > 0) {
            var type = rowObject.cChr_RuleType;
            var typevalue = rowObject.cChr_RuleValue;
            var returnStr = '';
            if (typevalue != null && typevalue != "") {
                var valuesplit = typevalue.split(';');
                var valuesplitlength = valuesplit.length;
                switch (type) {
                    case 'Dropdownlist':
                        for (var i = 1; i <= valuesplit.length; i++)
                            returnStr = returnStr + '<option  selected="" value="' + valuesplit[i - 1] + '">' + valuesplit[i - 1] + '</option>';
                        return '<select class="RcChr_Char01">' + returnStr + '</select>';
                    case 'CheckBox':
                        for (var i = 1; i < valuesplit.length; i++)
                            returnStr = returnStr + '<input class="RcChr_Char01" type="checkbox" value="' + valuesplit[i - 1] + '">' + valuesplit[i - 1] + '</input>';
                        return returnStr;
                    case 'RadioBox':
                        for (var i = 1; i < valuesplit.length; i++)
                            returnStr = returnStr + '<input class="RcChr_Char01" type="radio" name ="' + rowObject.RcChr_RuleName + rowObject.$id + '" value="' + valuesplit[i - 1] + '">' + valuesplit[i - 1] + '</input>';
                        return returnStr;
                }
            }
            else if (type == "Input")
                returnStr = '<input type="text"></input>'
            return returnStr;
        }
    }

    function RuleOperation(cellvalue, options, rowObject) {
        if (rowObject.cChr_Permission != "Public") {
            var returnStr = '<input class="BtnOpenRule Modify" title="编辑" type="button" onClick="RuleEdit(\'' + options.rowId + '\',\'' + rowObject.RequestID + '\')" value =""></input>&nbsp;&nbsp;'
            returnStr = returnStr + '<input class="Del" title="删除" type="button" onClick="DeleteClick2(\'' + options.rowId + '\',\'' + rowObject.RequestID + '\')" value =""></input>'
            return '<center>' + returnStr + '</center>';
        }
        else {
            return ""
        }
    };

    function DeleteClick2(str1, str) {
        //alert(str1, str);
        var data = {};
        data['cChr_Char03'] = "";
        $.ajax("/API/InspectionRuleAPI?id=" + str, {
            data: JSON.stringify(data),
            type: "PUT", contentType: "application/json",
            success: function (data) {
                //alert(data);
            }
        });
        $("#gridTable2").jqGrid('delRowData', str1);
    }

    function RuleRelat() {
        var returnStr = "";
        var returnRequest = "";
        var datalist = [];
        $("#gridTable2 .cbox").each(function (index, element) {
            if (element.checked) {
                @*var data = {};
                data['cChr_TicketID'] = '@ViewBag.TicketID';
                data['cChr_RuleID'] = jQuery('#gridTable2').getRowData(index + 1).RequestID;
                data['cChr_RuleName'] = jQuery('#gridTable2').getRowData(index + 1).cChr_RuleName;
                data['cChr_ObjectID'] = '@ViewBag.Name';
                data['cChr_Char03'] = '@ViewBag.Type';
                var mark = (element.checked == true ? "No" : "Yes");
                data['cChr_MarkAsDelete'] = mark;
                datalist.push(data);*@
                returnStr = returnStr + ";" + jQuery('#gridTable2').getRowData(index + 1).cChr_RuleName;
                returnRequest = returnRequest + ";" + jQuery('#gridTable2').getRowData(index + 1).RequestID;
            }
        });
        returnStr = (returnStr == "" ? " " : returnStr.substring(1));
        returnRequest = (returnRequest == "" ? " " : returnRequest.substring(1));
        DialogRDClose(returnStr, returnRequest, '@ViewBag.RowId');
        //if (datalist.length > 0) {
        //    $.ajax("/API/SMGXJPlanTicketRuleRelatedAPI/", {
        //        data: JSON.stringify(datalist),
        //        type: "POST", contentType: "application/json",
        //        async: false,
        //        success: function (data) {
        //        }
        //    });
        //}

        @*var data2 = {};
        data2['cChr_RelatedTicketID'] = '@ViewBag.TicketID';
        data2['cChr_DeviceName'] = '@ViewBag.Name';
        $.ajax("/API/CIAPI?id=" + '@ViewBag.TicketID', {
            data: JSON.stringify(data2),
            type: "PUT", contentType: "application/json",
            async: false,
            success: function (data) {
                if (data.indexOf('Fail') > 0)
                    alert(data);
                else {
                    DialogRDClose(data, '@ViewBag.RowId');
                }
            }
        });*@
    }

    function RuleAdd() {
        jQuery("#gridTable2").jqGrid("editGridRow", "new", {
            height: 300,
            reloadAfterSubmit: true,
            closeAfterAdd: true,
            url: '/API/InspectionRuleAPI/Post?type=' + '@ViewBag.Type' + "&Permission=" + '@ViewBag.Permission',
            async: false,
        }).jqGrid("setGridParam", {
            url: "/API/InspectionRuleAPI/GetRule", //设置表格的url
            datatype: "json", //设置数据类型
            async: false,
        }).trigger("reloadGrid");
        $("#cChr_RuleType").width('100').height('20');
    }

    function RuleEdit(rowid,RequestID) {
        var data = jQuery("#gridTable2").getRowData(rowid);
        jQuery("#gridTable2").jqGrid('editGridRow', rowid, {
            height: 300,
            recreateForm: true,
            closeAfterEdit: true,
            closeOnEscape: true,
            reloadAfterSubmit: false,
            mtype: "GET",
            url: '/API/InspectionRuleAPI/GetToModify?requestid=' + data.RequestID,
            async: false,
            onclickSubmit: function (params, posdata) {
                var data2 = {};
                data2['cChr_RuleType'] = posdata.cChr_RuleType;
                data2['cChr_RuleName'] = posdata.cChr_RuleName;
                data2['cChr_Threshold'] = posdata.cChr_Threshold;
                data2['cChr_RuleValue'] = posdata.cChr_RuleValue;
                $.ajax("/API/InspectionRuleAPI?id=" + jQuery("#gridTable2").getRowData(posdata.gridTable2_id).RequestID, {
                    data: JSON.stringify(data2),
                    type: "PUT", contentType: "application/json",
                    async: false,
                    success: function (data) {
                        //alert(data);
                    }
                });
            }
        })
        $(".navButton").hide();
        $("#cChr_RuleType").width('100').height('20');
    }

</script>
@*}*@
